package com.ruoyi.project.modules.interfaceTestLog.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.mapper.InterfaceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.interfaceTestLog.mapper.InterfaceTestLogMapper;
import com.ruoyi.project.modules.interfaceTestLog.domain.InterfaceTestLog;
import com.ruoyi.project.modules.interfaceTestLog.service.IInterfaceTestLogService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 接口测试日志接口Service业务层处理
 *
 * @author smallrain
 * @date 2022-06-27
 */
@Service
public class InterfaceTestLogServiceImpl implements IInterfaceTestLogService {
    @Resource
    private InterfaceTestLogMapper interfaceTestLogMapper;
    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    /**
     * 查询接口测试日志接口
     *
     * @param interfaceId 接口测试日志接口主键
     * @return 接口测试日志接口
     */
    @Override
    public InterfaceTestLog selectInterfaceTestLogByInterfaceId(Long interfaceId) {
        return interfaceTestLogMapper.selectInterfaceTestLogByInterfaceId(interfaceId);
    }

    /**
     * 查询接口测试日志接口列表
     *
     * @param interfaceTestLog 接口测试日志接口
     * @return 接口测试日志接口
     */
    @Override
    public List<InterfaceTestLog> selectInterfaceTestLogList(InterfaceTestLog interfaceTestLog) {
        return interfaceTestLogMapper.selectInterfaceTestLogList(interfaceTestLog);
    }

    /**
     * 新增接口测试日志接口
     *
     * @param interfaceTestLog 接口测试日志接口
     * @return 结果
     */
    @Override
    public int insertInterfaceTestLog(InterfaceTestLog interfaceTestLog) {
        return interfaceTestLogMapper.insertInterfaceTestLog(interfaceTestLog);
    }

    /**
     * 修改接口测试日志接口
     *
     * @param interfaceTestLog 接口测试日志接口
     * @return 结果
     */
    @Override
    public int updateInterfaceTestLog(InterfaceTestLog interfaceTestLog) {
        interfaceTestLog.setUpdateTime(DateUtils.getNowDate());
        return interfaceTestLogMapper.updateInterfaceTestLog(interfaceTestLog);
    }

    /**
     * 批量删除接口测试日志接口
     *
     * @param interfaceIds 需要删除的接口测试日志接口主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceTestLogByInterfaceIds(String interfaceIds) {
        return interfaceTestLogMapper.deleteInterfaceTestLogByInterfaceIds(Convert.toStrArray(interfaceIds));
    }

    /**
     * 删除接口测试日志接口信息
     *
     * @param interfaceId 接口测试日志接口主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceTestLogByInterfaceId(Long interfaceId) {
        return interfaceTestLogMapper.deleteInterfaceTestLogByInterfaceId(interfaceId);
    }

    @Override
    @Transactional
    public void saveInterfaceTestLog(InterfaceTestLog interfaceTestLog) {
        if (interfaceTestLog.getInterfaceId() == null) return ;
        interfaceTestLog.setUpdateTime(new Date());
        if(interfaceTestLog.getContent() == null){
            interfaceTestLog.setContent("编译没有问题！！！");
        }
        int i = this.interfaceTestLogMapper.updateInterfaceTestLog(interfaceTestLog);
        if (i == 0) {
            this.interfaceTestLogMapper.insertInterfaceTestLog(interfaceTestLog);
        }
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        interfaceInfo.setId(interfaceTestLog.getInterfaceId());
        interfaceInfo.setIsEditTest("N");
        if ("0".equals(interfaceTestLog.getStatus())) {
            interfaceInfo.setErrorStatus(interfaceTestLog.getErrorStatus());
        }else{
            interfaceInfo.setErrorStatus("10");
        }
        this.interfaceInfoMapper.updateInterfaceInfo(interfaceInfo);
    }

    @Override
    public void batchUpdateInterface(List<Long> interfaceIds, String lifeCycle) {
        this.interfaceInfoMapper.batchUpdateInterface(interfaceIds, lifeCycle);
    }
}